package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.ExternalContactLetter;
import com.amsp.oa.oademo.mapper.ExternalContactLetterMapper;
import com.amsp.oa.oademo.service.ExternalContactLetterService;
import com.amsp.oa.oademo.vo.ExternalContactLetterVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 外部联络函Service实现类
 */
@Service
public class ExternalContactLetterServiceImpl extends ServiceImpl<ExternalContactLetterMapper, ExternalContactLetter>
        implements ExternalContactLetterService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateExternalContactLetter(ExternalContactLetterVO vo) {
        ExternalContactLetter entity = new ExternalContactLetter();
        BeanUtils.copyProperties(vo, entity);

        if (entity.getId() == null) {
            entity.setCreateTime(LocalDateTime.now());
        }
        entity.setUpdateTime(LocalDateTime.now());

        return saveOrUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSaveOrUpdateExternalContactLetter(List<ExternalContactLetterVO> voList) {
        if (voList == null || voList.isEmpty()) {
            return false;
        }

        List<ExternalContactLetter> entityList = voList.stream().map(vo -> {
            ExternalContactLetter entity = new ExternalContactLetter();
            BeanUtils.copyProperties(vo, entity);

            if (entity.getId() == null) {
                entity.setCreateTime(LocalDateTime.now());
            }
            entity.setUpdateTime(LocalDateTime.now());

            return entity;
        }).collect(Collectors.toList());

        return saveOrUpdateBatch(entityList);
    }

    @Override
    public ExternalContactLetterVO getExternalContactLetterDetail(Long id) {
        if (id == null) {
            return null;
        }

        ExternalContactLetter entity = getById(id);
        if (entity == null) {
            return null;
        }

        ExternalContactLetterVO vo = new ExternalContactLetterVO();
        BeanUtils.copyProperties(entity, vo);

        return vo;
    }

    @Override
    public Page<ExternalContactLetterVO> getExternalContactLetterPage(Page<ExternalContactLetter> page,
            ExternalContactLetterVO vo) {
        LambdaQueryWrapper<ExternalContactLetter> queryWrapper = new LambdaQueryWrapper<>();

        // 构建查询条件
        if (vo != null) {
            if (StringUtils.hasText(vo.getToUnit())) {
                queryWrapper.like(ExternalContactLetter::getToUnit, vo.getToUnit());
            }

            if (StringUtils.hasText(vo.getFromSender())) {
                queryWrapper.like(ExternalContactLetter::getFromSender, vo.getFromSender());
            }

            if (StringUtils.hasText(vo.getRecipients())) {
                queryWrapper.like(ExternalContactLetter::getRecipients, vo.getRecipients());
            }

            if (vo.getIssueDate() != null) {
                queryWrapper.eq(ExternalContactLetter::getIssueDate, vo.getIssueDate());
            }

            if (StringUtils.hasText(vo.getLetterNumber())) {
                queryWrapper.like(ExternalContactLetter::getLetterNumber, vo.getLetterNumber());
            }
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(ExternalContactLetter::getCreateTime);

        // 执行分页查询
        Page<ExternalContactLetter> entityPage = page(page, queryWrapper);

        // 转换为VO对象
        Page<ExternalContactLetterVO> voPage = new Page<>();
        voPage.setCurrent(entityPage.getCurrent());
        voPage.setSize(entityPage.getSize());
        voPage.setTotal(entityPage.getTotal());

        List<ExternalContactLetterVO> voList = new ArrayList<>();
        for (ExternalContactLetter entity : entityPage.getRecords()) {
            ExternalContactLetterVO letterVO = new ExternalContactLetterVO();
            BeanUtils.copyProperties(entity, letterVO);
            voList.add(letterVO);
        }

        voPage.setRecords(voList);

        return voPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteExternalContactLetter(Long id) {
        if (id == null) {
            return false;
        }

        return removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteExternalContactLetter(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }

        return removeByIds(ids);
    }
}